package goods;

import java.util.ArrayList;

public class GoodsDAOArrayList {
    private ArrayList<Goods> goodsArrayList = new ArrayList<>();


    void addGoods(Goods G) {
        goodsArrayList.add(G);
    }
    public String toString(){
        return goodsArrayList.toString();
    }
    public ArrayList<Goods> findGoods(String label){
        ArrayList<Goods> FG = new ArrayList<>();
        for (Goods G:goodsArrayList) {
            int flag = 0;
            for (String L:G.getLabel()) {
                if(L.equals(label)){
                    FG.add(G);
                    flag = 1;
                    break;
                }
            }
            if(G.getName().contains(label)&&flag == 0){
                FG.add(G);
            }
        }
        return FG;
    }



    public Goods checkTheItemById(String id) {
        long item = Long.parseLong(id);
        for (Goods e:goodsArrayList) {
            if(e.getId()==item){
                return e;
            }
        }
        return null;
    }


    public String displayInformation(Goods G) {
        return G.getName()+"    "+G.getPrice()+"    "+G.getAmount()+"   "+G.getId();
    }


    public boolean deleteGoods(String id) {
        for(int i = 0 ; i < goodsArrayList.size() ; i ++ ){
            if(goodsArrayList.get(i).getId()==Long.parseLong(id)){
                goodsArrayList.remove(i);
                return true;
            }
        }
        return false;
    }

    public String returnMaximumId() {
        return ""+goodsArrayList.get(goodsArrayList.size()-1).getId();
    }

    public ArrayList<Goods> getGoodsArrayList() {
        return goodsArrayList;
    }
}
